
#' Code to replace membership information of informal IOs that are in COW-IGO v3
#' and to then save a new version of COW-IGO v3 that is only the formal IOs
#' This "formals only" dataset can then be used alongside the Informal IOs dataset
#' to avoid double counting IO memberships

library(tidyverse)
library(haven)
library(readxl)

# setwd("Informal-IOs-dataset-2021-june")

# Load in the COW v3 data to make the formals only subset from COW

cow_v3 <- read_dta("cow_v3.dta") %>% 
  pivot_longer(names_to = "key", values_to = "member", aaaid:wassen)

# Load in the informals master data to use to identify IOs that formalize

informals_master <- read_excel("informals_master.xlsx", 
                               sheet = "Informals", range = "A1:G261") %>% 
  mutate(abb = as.character(tolower(Name))) %>%
  select(abb, 
         full_name = `Full Name`, 
         created = Established,
         terminated = Terminated, 
         formalized = Formalized, 
         in_cow = Details) %>% 
  filter(!is.na(full_name)) %>% 
  mutate(in_cow = ifelse(!is.na(in_cow), 1, 0),
         formalized = ifelse(is.na(formalized), 0, formalized)) %>% 
  # Change some abbreviations to match XLSX names
  mutate(abb = if_else(abb=="3g", "ggg", abb),
         abb = if_else(abb=="afrosai-e", "afrosai_e", abb),
         abb = if_else(abb=="bimp-eaga", "bimp_eaga", abb),
         abb = if_else(abb=="cmim/amro", "cmimamro", abb),
         abb = if_else(abb=="coomets", "coomet", abb),
         abb = if_else(abb=="fsf/fsb", "fsb", abb),
         abb = if_else(abb=="g7/8", "g7", abb),
         abb = if_else(abb=="ghtf/imdrf", "imdrf_ghtf", abb),
         abb = if_else(abb=="gifcs/ogbs", "ogbs", abb),
         abb = if_else(abb=="giics/ogis", "giics", abb),
         abb = if_else(abb=="imt-gt", "imt_gt", abb),
         abb = if_else(abb=="ior-arc", "iorarc", abb),
         abb = if_else(abb=="eurodrug", "eurodrugs", abb)) %>% 
  # Change some abbreviations to match COW names
  mutate(abb = if_else(abb=="asba", "asblac", abb),
         abb = if_else(abb=="cs", "comsec", abb), # Will want to change back
         abb = if_else(abb=="csce/osce", "osce", abb), # CSCE informal 1973 to 1994, OSCE 1995 -> formal
         abb = if_else(abb=="cp", "colombo", abb), # Will want to change back 
         abb = if_else(abb=="icpc", "interpol", abb), # Will want to change back
         abb = if_else(abb=="nc", "nordc", abb), # Will want to change back
         abb = if_else(abb=="ctb", "cto", abb), 
         abb = if_else(abb=="vasabs", "vasab", abb), 
         abb = if_else(abb=="ciss", "iacss", abb),
         abb = if_else(abb=="aihja", "iasaj", abb),
         abb = if_else(abb=="ppd", "pipd", abb),
         abb = if_else(abb=="spf/pif", "pif", abb),
         abb = if_else(abb=="spd", "swpd", abb)) %>% 
  # Fix COCOM
  add_row(abb = "cocom", 
          full_name = "Coordination Committee on Multilateral Export Controls", 
          created = 1945, 
          terminated = 1994, 
          formalized = 1,
          in_cow = 0) %>% 
  filter(abb != "cocom/wa") %>% 
  # Change CSCE established data
  mutate(created = if_else(abb == "osce", 1973, created))

# Merge informals start/end/formalize dates with COW v3
cow_formals_subset <- left_join(cow_v3, 
                                informals_master %>% 
                                  select(key = abb, terminated, formalized) %>% 
                                  mutate(is_informal = 1),
                                by = "key") %>% 
  arrange(key, year, ccode) %>% 
  # Replace member == -7 if IO is actually informal in that year
  mutate(terminated = ifelse(is.na(terminated), 2015, terminated),
         is_informal = ifelse(is.na(is_informal), 0, is_informal)) %>% 
  mutate_at(vars(member), ~replace(., (formalized == 1 & year < terminated), -7)) %>% 
  mutate_at(vars(member), ~replace(., (formalized == 0 & is_informal == 1), -7)) %>% 
  # Switch the keys/IO abbreviations
  mutate(key = if_else(key == "comsec", "cs", key),
         key = if_else(key == "colombo", "cp", key),
         key = if_else(key == "interpol", "icpc", key),
         key = if_else(key == "nordc", "nc", key)) %>% 
  # Drop extra variables
  select(-state, -terminated, -formalized, -is_informal) %>% 
  # Reshape to panel
  pivot_wider(names_from = "key",
              values_from = "member") %>% 
  arrange(ccode, year) %>% 
  as.data.frame()


# Save cow_formals_subset
# ==
write_csv(cow_formals_subset, file = "formals_v3.csv")
# write_dta(cow_formals_subset, path = "formals_v3.dta", version = 12)



